<?php
class UsersController extends AppController{
	var $name = "Users";
	var $helpers = array("Html","Session");
	var $components = array("Session");

	function login (){
		$this->layout = 'login_site';
		if ($this->Auth->user()) {
			$employee_id = $this->User->query("SELECT e.id FROM employees e JOIN users u ON e.user_id = u.id WHERE u.id =".$this->Auth->user("id"));
			$this->User->query("INSERT INTO audit(employee_id,login) VALUES (".$employee_id[0]["e"]["id"].",NOW())");
			$this->redirect($this->Auth->redirect("/managers/index"));
		}
	}

	function beforeFilter(){
		parent::beforeFilter();
	}

	function admin_add() {
		if(!empty($this->data)){

			$this->User->set($this->data);
			if($this->User->validateUser()){
				$this->User->save($this->data);
				$this->Session->setFlash("You has been add new User !");
				$this->redirect("/admin/users");
			}

		}else{
			$this->render();

		}
	}
	/**
	 * Dang xuat
	 */
	function logout(){
		$audit_id = $this->User->query("SELECT id FROM audit WHERE employee_id = (SELECT e.id FROM employees e JOIN users u ON e.user_id = u.id WHERE u.id =".$this->Auth->user("id").") ORDER BY login DESC LIMIT 0,1");
			$this->User->query("UPDATE audit SET logout = NOW() WHERE id=".$audit_id[0]["audit"]["id"]);
		$this->redirect($this->Auth->logout());
	}
}
?>